//
//  MLXPopUpManager.m
//  Pods-MLXPopUpManager
//
//  Created by SY on 2019/3/25.
//

#import "MLXPopUpManager.h"
#import "MLXPopUpCustomView.h"

static MLXPopUpManager *_instance;

@interface MLXPopUpManager ()

@property(nonatomic,strong) dispatch_queue_t popQueue;

@property(nonatomic,strong) NSMutableArray *lows;

@property(nonatomic,strong) NSMutableArray *defaults;

@property(nonatomic,strong) NSMutableArray *heights;

@property(nonatomic,assign) BOOL isPop;

@end

@implementation MLXPopUpManager
+(instancetype)shareManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
-(instancetype)init{
    if (self = [super init]) {
        _popQueue = dispatch_queue_create("com.mlx.alert", DISPATCH_QUEUE_SERIAL);
       
    }
    return self;
}
-(void)showCustomView:(MLXPopUpCustomView *)customView popupLevelType:(MLXPopUpLevelType)popupLevelType{
    if (!customView || ![customView isKindOfClass:MLXPopUpCustomView.class]) return;
    [self removeObject:customView];
    if (popupLevelType == MLXPopUpLevelLowType) {
        [self.lows addObject:customView];
    }else if (popupLevelType == MLXModuleGuideDefaultType){
        [self.defaults addObject:customView];
    }else if (popupLevelType ==  MLXModuleGoodsHightType){
        [self.heights addObject:customView];
    }else{
        return;
    }
    [self show];
}
-(void)setSuspend:(BOOL)suspend{
    _suspend = suspend;
    if (!suspend) {
        [self show];
    }
}
-(void)show{
    if (self.isPop || self.suspend)  return;
    MLXPopUpCustomView *view = [self getPopView];
    if (!view) {
        self.isPop = NO;
        return;
    }
    self.isPop = YES;
    dispatch_async(self.popQueue, ^{
        //创建一个初始为0的信号量
        dispatch_async(dispatch_get_main_queue(), ^{
            !view.prepareShowBlock ?: view.prepareShowBlock(view);
            [view setViewData];
        });
    });
}
-(void)dismiss:(MLXPopUpCustomView *)view{
    [self removeObject:view];
    self.isPop = NO;
    [self show];
}
-(MLXPopUpCustomView *)getPopView{
     MLXPopUpCustomView *view;
    if (self.heights.count) {
        view = self.heights.firstObject;
    }else if (self.defaults.count){
        view = self.defaults.firstObject;
    }else if (self.lows.count){
        view = self.lows.firstObject;
    }
    return view;
}
-(void)removeObject:(UIView *)view{
    if ([self.lows containsObject:view]) {
        [self.lows removeObject:view];
    }else if ([self.defaults containsObject:view]){
        [self.defaults removeObject:view];
    }else if ([self.heights containsObject:view]){
        [self.heights removeObject:view];
    }
}
-(NSMutableArray *)lows{
    if (!_lows) {
        _lows = [NSMutableArray array];
    }
    return _lows;
}
-(NSMutableArray *)defaults{
    if (!_defaults) {
        _defaults = [NSMutableArray array];
    }
    return _defaults;
}
-(NSMutableArray *)heights{
    if (!_heights) {
        _heights = [NSMutableArray array];
    }
    return _heights;
}
@end
